#include"head.h"
#include"common.h"
#include"ip.h"
#include"nat.h"
#include"conn.h"
struct KernelResponse getAllConns(void) {
	struct cmdRequest req;
	
	printf("reach here getall");
			fflush(stdout);
	
	// exchange msg
	req.tp = REQ_GETAllConns;
	return exMsg(&req, sizeof(req));
}
int showOneConn(struct ConnLog log) {
	struct tm * timeinfo;
	char saddr[25],daddr[25],proto[6];
	// ip
	IPint2IPstrWithPort(log.saddr,log.sport,saddr);
	IPint2IPstrWithPort(log.daddr,log.dport,daddr);
	// protocol
	if(log.protocol == IPPROTO_TCP) {
		sprintf(proto, "TCP");
	} else if(log.protocol == IPPROTO_UDP) {
		sprintf(proto, "UDP");
	} else if(log.protocol == IPPROTO_ICMP) {
		sprintf(proto, "ICMP");
	} else if(log.protocol == IPPROTO_IP) {
		sprintf(proto, "any");
	} else {
		sprintf(proto, "other");
	}
	printf("| %-5s |  %21s |->|  %21s | Established |\n",proto, saddr, daddr);
	if(log.natType == NAT_TYPE_SRC) {
		IPint2IPstrWithPort(log.nat.daddr, log.nat.dport, saddr);
		printf("| %-5s |=>%21s |->|  %21c | %11c |\n", "NAT", saddr, ' ', ' ');
	} else if(log.natType == NAT_TYPE_DEST) {
		IPint2IPstrWithPort(log.nat.daddr, log.nat.dport, daddr);
		printf("| %-5s |  %21c |->|=>%21s | %11c |\n", "NAT", ' ', daddr, ' ');
	}

}

int showConns(struct ConnLog *logs, int len) {
	int i;
	printf("reach here show conn");
			fflush(stdout);
	if(len == 0) {
		printf("No connections now.\n");
		return 0;
	}
	// printf("connection num: %d\n", len);
	printLine();
	printf("| %-5s |  %21s |->|  %21s | %11s |\n", "Protocol", "src IP", "dst IP", "status");
	printLine();
	for(i = 0; i < len; i++) {
		showOneConn(logs[i]);
	}
	printLine();
	return 0;
}